import React, {Component} from 'react';
import {Text, TouchableOpacity, View,} from 'react-native';

import {positionsevenStyle as styles} from '../../../../../styles';
import Icon from 'react-native-vector-icons/Ionicons';
import {fetchRequest} from "../../../../util/fetch";
import {myAlert} from '../../../../util/compatible';


export default class Posseven extends Component {
    // 初始化模拟数据
    constructor(props) {            //构造器只执行一次
        super(props);
        this.state = {
            collectState: this.props.collectState,
            sendState: 0,
            resumeCompletion: 0
        }
    };

    componentDidMount() {
        fetchRequest('Resume/selectResumeCompletion.go', 'POST', {}).then(
            res => this.setState({resumeCompletion: res.resumeCompletion})
        );
    }

    render() {
        if (this.props) {
            return (
                <View style={styles.posseven}>
                    <View style={styles.possevenin1}>
                        <TouchableOpacity
                            activeOpacity={1}
                            onPress={() => {
                                this.goTo()
                            }}
                        >
                            {this.props.collectState === 'ok' ?
                                <View>
                                    <Icon
                                        name={'md-heart'}
                                        size={25}
                                        color={'#fc6866'}
                                        style={styles.possevenicon}/>
                                    <Text style={styles.possp2}>已收藏</Text>
                                </View>
                                :
                                <View>
                                    <Icon
                                        name={'md-heart-outline'}
                                        size={25}
                                        color={'#aeaeae'} style={styles.possevenicon}/>

                                    <Text style={styles.possp}>收藏职位</Text>
                                </View>
                            }

                        </TouchableOpacity>
                    </View>
                    {this.props.selisSend === 'ok' ?
                        <TouchableOpacity style={[styles.possevenin, {backgroundColor: '#aeaeae'}]} activeOpacity={1}>
                            <Text style={styles.seventext}> 已投递</Text>
                        </TouchableOpacity>
                        :
                        this.props.selisSend === 'err' ?
                            <TouchableOpacity
                                style={styles.possevenin}
                                activeOpacity={1}
                                onPress={() => this.commit()}
                            >
                                <Text style={styles.seventext}> 我感兴趣请联系我</Text>
                            </TouchableOpacity>
                            :
                            null
                    }
                </View>
            )
        }
    }

    goTo() {
        if (this.state.collectState === this.props.collectState) {
            if (this.state.collectState === 'ok') {
                this.props.actions.DeleteCollectAction({postionId: this.props.postionId});
                this.setState({collectState: 'err'});
            } else if (this.state.collectState === 'err') {
                this.props.actions.InsertCollectAction({postionId: this.props.postionId});
                this.setState({collectState: 'ok'});
            }
        }
    }

    commit() {
        if (this.state.resumeCompletion <= 80) {
            myAlert('请完善简历');
        } else if (this.state.sendState === 0) {
            this.props.actions.SendPostionAction({postionId: this.props.postionId});
            this.setState({sendState: 1});
        }
    }

}